<?php

namespace App\Libraries\Util;

use Ramsey\Uuid\Uuid;

class UuidHelper
{
    const UUID_MATCH = '/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/';

    /**
     * 编号是否为Uuid.
     *
     * @param string $id
     *
     * @return bool
     */
    public static function IsUuid($id)
    {
        if (is_null($id) || empty($id)) {
            return false;
        }

        return (bool) preg_match(static::UUID_MATCH, $id);
    }

    /**
     * 生成Uuid.
     *
     * @return \Ramsey\Uuid\UuidInterface
     *
     * @throws \Exception
     */
    public static function GenerateUuid()
    {
        return Uuid::uuid4();
    }

    /**
     * 生成Uuid并转为字符串格式.
     *
     * @return string
     *
     * @throws \Exception
     */
    public static function GenerateUuidToString()
    {
        return Uuid::uuid4()->toString();
    }
}
